$(function () {
    $(function () {

        //console.log(getQueryString('categoryid'));
        setProduckTitle();
        getproductlist();
    });
    /*获取url中的参数*/
    function getQueryString(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
        //console.log(r);
        if (r != null) {
            return decodeURI(r[2]);
        }
        return null;
    }
    /*获取字符串中的数字*/
    function getNum(text) {
        var value = text.replace(/[^0-9]/ig, "");
        //alert(value);
        return value;
    }
    /*设置标题中的文字*/
    function setProduckTitle() {

        var title = getQueryString('category');
        //console.log(title);
        //title = decodeURI(title);
        $('#productlist>.product-title>.breadcrumb>li.active').html(title);
    }
    /*获取商品列表*/
    function getproductlist() {
        var categoryid = getQueryString('categoryid');
        $.get('http://139.199.157.195:9090/api/getproductlist?categoryid=' + categoryid, function (result) {
            //console.log(result);
            var html = template('lsitTpl', result);

            //console.log(html);
            $('#productlist>.product-list>ul').html(html);
            /*获取页面中的页码*/
            var page = parseInt(result.totalCount / result.pagesize);
            //console.log(page);
            /*新建页码数组*/
            var pageArr = [];
            /*判断如果page==0则还是会有一页*/
            if( page == 0)  page = 1;
            for (var i = 1; i <= page; i++) {
                pageArr.push(i);
            }
            // console.log(pageArr);
            /*新建页码对象*/
            var pageObj = {
                'pageArr': pageArr
            }
            var pageHtml = template('pageTpl', pageObj);
            //console.log(pageHtml);
            $('#productlist>.product-page>.dropdown>ul').html(pageHtml);
            /*页码点击事件*/
            $('#productlist>.product-page>.dropdown>ul>li').on('click', function () {
                /*获取li标签的page属性*/
                var pageid = $(this).data('page');
                var $that = $(this);
                //console.log(pageid);
                $.get('http://139.199.157.195:9090/api/getproductlist?categoryid=' + categoryid + '&pageid=' + pageid, function (result) {
                    var html = template('lsitTpl', result);

                    //console.log(html);
                    $('#productlist>.product-list>ul').html(html);
                    var pageText = $that.children('a').html();
                    //console.log(pageText);
                    $('#productlist>.product-page>.dropdown>button:first-child').html(pageText + '<span class="caret"></span>');
                })
            })
            /*上一页点击事件*/
            $('#productlist>.product-page>input:first-child').on('click', function () {
                /*获取button标签的page页码*/
                var $pageText = $('#productlist>.product-page>.dropdown>button:first-child').html();
                // console.log($pageText);
                var pageid = getNum($pageText);
                //console.log(pageNum);
                /*判断页面数字是否为1*/
                if (pageid == 1) return;
                pageid--;
                $.get('http://139.199.157.195:9090/api/getproductlist?categoryid=' + categoryid + '&pageid=' + pageid, function (result) {
                    window.scrollTo(0,0);
                    var html = template('lsitTpl', result);

                    //console.log(html);
                    $('#productlist>.product-list>ul').html(html);
                    $('#productlist>.product-page>.dropdown>button:first-child').html('第 '+pageid+' 页' + '<span class="caret"></span>');
                })
            })
            /*下一个点击事件*/
            $('#productlist>.product-page>input:last-child').on('click', function () {
                /*获取button标签的page页码*/
                var $pageText = $('#productlist>.product-page>.dropdown>button:first-child').html();
                // console.log($pageText);
                var pageid = getNum($pageText);
                /*获取页码最大值*/
                var maxPageid = $('#productlist>.product-page>.dropdown>ul>li').length;
                //console.log(maxPageid);
                //console.log(pageNum);
                /*判断页面数字是否为最后一页*/
                if (pageid == maxPageid) return;
                pageid++;
                $.get('http://139.199.157.195:9090/api/getproductlist?categoryid=' + categoryid + '&pageid=' + pageid, function (result) {
                    window.scrollTo(0,0);                    
                    var html = template('lsitTpl', result);

                    //console.log(html);
                    $('#productlist>.product-list>ul').html(html);
                    $('#productlist>.product-page>.dropdown>button:first-child').html('第 '+pageid+' 页' + '<span class="caret"></span>');                    
                })
            })
        })

    }
})

